package com.miyake.loveviewweb.shared;

import java.util.ArrayList;
import java.util.List;

public class XsvToColumnArrayConverter {
    public List<ArrayList<String>> getColumns() {
        return columns;
    }

    private List<ArrayList<String>> columns = new ArrayList<ArrayList<String>>();
    
    public XsvToColumnArrayConverter(String text) {
        String splitter;
        
        String cr = " ";
        if (text.contains("\r\n")) {
            cr = "\r\n";
        }
        else if (text.contains("\n")) {
            cr = "\n";
        }
        String[] lines = text.split(cr);
        
        String[] firstRow = null;
        if (lines[0].contains(",")) {
            splitter = ",";
        }
        else if (lines[0].contains("\t")) {
            splitter = "\t";
        }
        else {
            splitter = " ";
            
        }
        firstRow = lines[0].split(splitter);
        
        for (int i = 0; i < firstRow.length; i++) {
            columns.add(new ArrayList<String>());
        }
        
        for (String row : lines) {
            String[] col = row.split(splitter);
             
            for (int i = 0; i < col.length; i++) {
                columns.get(i).add(col[i]);
            }
        }       
    }
    
    public int getColumnCount() {
        return columns.size();
    }
}
